#include "gesture.h"
#include "log.h"
#include <math.h>

#define DELTA_ANGLE 5.0f

UFO_Direction ufoGestureCalDircetion(ufoPoint *point, int count)
{
	UFO_Direction dir = 0;
    if (point && count >= 2) {
		float x1 = 0;
		float y1 = 1;
		
		float x2 = -point[count-1].x+point[0].x;
		float y2 = -point[count-1].y+point[0].y;

		float angle = acosf((x1*x2 + y1*y2) / (sqrtf(x1*x1 + y1*y1)*sqrtf(x2*x2 + y2*y2)))*180.0f/3.1415926f;

		if (y2 > 0) {
			if (angle < 60.0f)
				dir |= UFO_TOP;
		}
		else {
			if (angle > 120)
				dir |= UFO_BOTTOM;			
		}

		if (x2 > 0) {
			if (angle > (0+DELTA_ANGLE) && angle < (180.0f-DELTA_ANGLE))
				dir |= UFO_LEFT;
		}
		else {
			if (angle > (0+DELTA_ANGLE) && angle < (180.0f-DELTA_ANGLE))
				dir |= UFO_RIGHT;			
		}
    }
    return dir;
}

ufoPoint ufoGestureCalcSpeed(ufoPoint *point, int count, long deltaTime)
{
    ufoPoint pt = {0};
    pt.x = fabs(point[count-1].x-point[0].x)/deltaTime;
    pt.y = fabs(point[count-1].y-point[0].y)/deltaTime;
    return pt;
}


